﻿using DataWorks_Tools.BasicMethods;
using DataWorks_Tools.MappModals;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;


namespace DataWorks_Tools.MergeDetails
{
    public static class MergeDetailsMethods
    {
        /// <summary>
        ///（专用于边缘采集器） 获得需要处理的日期文件夹列表
        /// </summary>
        /// <param name="originalpath">源数据路径</param>
        /// <param name="combinedpath">指定的合并后的路径</param>
        /// <param name="configuredfolders">指定需要合并的日期文件夹，这是在配置文件读取到的日期数据</param>
        /// <returns></returns>
        public static DirectoryInfo[] YXHGetProcessedDateDir(string odpath, string rdpath, string[] datedir)
        {
            //获取input路径所有的子文件夹
            var subdirectorieslist = BasicFilesOperation.YXHGetSubDirectories(odpath);
            //获取目标路径所有的子文件夹
            var subtargetdirectorieslist = BasicFilesOperation.YXHGetSubDirectories(rdpath);
            //获取指定合并路径下所有已经存在的子文件夹名
            var targetlist = subtargetdirectorieslist.Select(s => s.Name).ToArray();
            DirectoryInfo[] specialdirectory;

            //如为null或者为all，则表明需要合并所有的originalpath下的子文件夹，如不为null，则表明需要合并指定的日期文件夹
            if (datedir != null && !datedir.Contains("all"))
            {
                specialdirectory = subdirectorieslist.Where(s => datedir.Contains(s.Name)).ToArray();
            }
            else
            {
                specialdirectory = subdirectorieslist;
            }
            if (targetlist.Length > 0)
            {
                //选择目标路径里没有的文件夹，如有说明之前可能已经合并过了，不需要合并，减少合并时间
                specialdirectory = specialdirectory.Where(s => !targetlist.Contains(s.Name)).ToArray();
            }
            return specialdirectory;
        }

        /// <summary>
        ///（专用于边缘采集器） 返回一组以30分钟来分割的时间线，如（2022-07-11 00:00:00），一共有24个
        /// </summary>
        /// <param name="file">FileInfo类型文件</param>
        /// <returns>List<DateTime></returns>
        public static List<DateTime> YXHGetSplitTime(this FileInfo file)
        {
            List<DateTime> list = new List<DateTime>();
            //从文件名获得日期，不需要时刻
            string date = file.YXHFilenametoDate();
            for (int i = 0; i < 24; i++)
            {
                list.Add(Convert.ToDateTime(date + " " + i.ToString("D2") + ":00:00"));
                list.Add(Convert.ToDateTime(date + " " + i.ToString("D2") + ":30:00"));
            }
            list.Add(Convert.ToDateTime(date + " " + "23:59:59"));
            return list;
        }

        /// <summary>
        /// 从csv名称中判断需要删除的csv的index的合集，因为一组数据如果只有61个csv就说明有问题，直接删除
        /// </summary>
        /// <param name="data">csv文件名中的序号集合</param>
        /// <returns></returns>
        public static List<List<int>> YXHGetCsvIndex(this List<int> data)
        {
            List<List<int>> result = new List<List<int>>();
            for (int i = 1; i < data.Count; i++)
            {
                if (data[i] == 1)
                {
                    if (data[i - 1] < 61)
                    {
                        //这是每个1前面的那个数的值，如果小与61，则把之前1到这个数之间的index记录下来
                        var t = data[i - 1];
                        //这个是这个数的index
                        var end = i - 1;
                        var start = i - t;
                        List<int> list = new List<int>();
                        for (int j = start; j <= end; j++)
                        {
                            list.Add(j);
                        }
                        result.Add(list);
                    }
                }
            }
            return result;
        }

        [Obsolete]
        /// <summary>
        ///（专用于边缘采集器）按照TXT来分类并返回分类好的CSV文件组版本，统一为MileageClassifyClass类，有起始终止index等信息
        /// </summary>
        /// <param name="allfile">FileInfo类型文件组</param>
        /// <returns></returns>
        public static List<MergeClassifyModal> YXHClassifyFilebyTxtOld(this FileInfo[] allfile)
        {
            //创建一个totallist，用于存储每2个txt中间的数据的list
            List<MergeClassifyModal> totallist = new List<MergeClassifyModal>();
            List<int> txtindex = new List<int>();
            List<int> csvindex = new List<int>();
            for (int i = 0; i < allfile.Length; i++)
            {
                if (allfile[i].Extension.ToLower() == ".txt")
                {
                    txtindex.Add(i);
                }
                if (allfile[i].Extension.ToLower() == ".csv")
                {
                    csvindex.Add(i);
                }
            }
            //txt有1个以上
            if (txtindex.Count > 0)
            {
                //添加第一个list，用于存放第一个txt之前的csv数据，不管有没有csv
                totallist.Add(new MergeClassifyModal());
                for (int j = 0; j < txtindex[0]; j++)
                {
                    totallist[0].filelist?.Add(allfile[j]);
                    totallist[0].endrowindex = 0;
                    totallist[0].startrowindex = 0;
                    totallist[0].endfileindex = 0;
                    totallist[0].startfileindex = 0;
                    totallist[0].endfileextramileage = 0;
                }
                for (int i = 0; i < txtindex.Count - 1; i++)
                {

                    //如果第一个数的后一个数比第二个数前一个数小，就说明中间有csv需要合并
                    if (txtindex[i] + 1 <= txtindex[i + 1] - 1)
                    {
                        //中间有csv才添加，否则全是0的数据
                        totallist.Add(new MergeClassifyModal());
                        for (int j = txtindex[i] + 1; j <= txtindex[i + 1] - 1; j++)
                        {
                            totallist[totallist.Count - 1].filelist?.Add(allfile[j]);//这里totallist[i+1]是因为totallist[0]已经有了，必须从1开始
                            totallist[totallist.Count - 1].endrowindex = 0;
                            totallist[totallist.Count - 1].startrowindex = 0;
                            totallist[totallist.Count - 1].endfileindex = 0;
                            totallist[totallist.Count - 1].startfileindex = 0;
                            totallist[totallist.Count - 1].endfileextramileage = 0;
                        }
                    }
                }
                //判断最后一个txt的序号是否已经大于csv的文件总数了，说明后面没有csv了，就不要再添加了
                if (txtindex[txtindex.Count - 1] < csvindex[csvindex.Count - 1])
                {
                    //添加最后一个list，用于存放最后一个txt之后的csv数据，不管有没有csv
                    totallist.Add(new MergeClassifyModal());
                    for (int j = txtindex[txtindex.Count - 1] + 1; j < allfile.Length; j++)
                    {
                        totallist[totallist.Count - 1].filelist?.Add(allfile[j]);
                        totallist[totallist.Count - 1].endrowindex = 0;
                        totallist[totallist.Count - 1].startrowindex = 0;
                        totallist[totallist.Count - 1].endfileindex = 0;
                        totallist[totallist.Count - 1].startfileindex = 0;
                        totallist[totallist.Count - 1].endfileextramileage = 0;
                    }
                }

            }
            //如果没有txt，那就直接把所有的csv文件放在totallist[0]里进行合并
            else
            {
                totallist.Add(new MergeClassifyModal());
                for (int j = 0; j < allfile.Length; j++)
                {
                    totallist[0].filelist?.Add(allfile[j]);
                    totallist[0].endrowindex = 0;
                    totallist[0].startrowindex = 0;
                    totallist[0].endfileindex = 0;
                    totallist[0].startfileindex = 0;
                    totallist[0].endfileextramileage = 0;
                }
            }
            return totallist;
        }
        [Obsolete]
        /// <summary>
        /// 此方法不考虑txt文件，直接利用csv文件名来合并停车间隔(这个经验证有bug，不能用）
        /// </summary>
        /// <param name="allfile"></param>
        /// <returns></returns>
        public static List<MergeClassifyModal> YXHClassifyFilebyTxtV2(this FileInfo[] csvfiles,int continuouscount)
        {
            List<MergeClassifyModal> totallist = new List<MergeClassifyModal>();
            List<int> index = new List<int>();
            //把每一个文件名中的序号存进index
            foreach (var csv in csvfiles)
            {
                var idx = csv.Name.Split('-')[2];
                var t = int.TryParse(idx, out int num);
                if (t)
                {
                    index.Add(num);
                }
                else
                {
                    return new List<MergeClassifyModal>();
                }
            }
            //获得所有1的索引
            var firstindex=index.Select((value, index) => new { value, index }).Where(pair => pair.value == 1).Select(pair => pair.index).ToList();
            if(firstindex.Count > 0)
            {
                for (int i = 0; i < firstindex.Count - 1; i++)
                {
                    
                    List<FileInfo> filelist = new List<FileInfo>();
                    for (int j = firstindex[i]; j < firstindex[i + 1]; j++)
                    {
                        filelist.Add(csvfiles[j]);
                    }
                    //连续超过60个csv的才能算有效数据
                    if(filelist.Count > continuouscount)
                    {
                        var m = new MergeClassifyModal();
                        m.filelist = filelist;
                        m.endrowindex = 0;
                        m.startrowindex = 0;
                        m.startfileindex = 0;
                        m.endfileindex = 0;
                        m.endfileextramileage = 0;
                        totallist.Add(m);
                    }
                   
                }
                //最后一个1后面还有文件没有算，别忘了
                if (firstindex[firstindex.Count - 1] < csvfiles.Length)
                {
                    var m = new MergeClassifyModal();
                    List<FileInfo> filelist = new List<FileInfo>();
                    for (int j = firstindex[firstindex.Count - 1]; j < csvfiles.Length; j++)
                    {
                        filelist.Add(csvfiles[j]);
                    }
                    m.filelist = filelist;
                    m.endrowindex = 0;
                    m.startrowindex = 0;
                    m.startfileindex = 0;
                    m.endfileindex = 0;
                    m.endfileextramileage = 0;
                    totallist.Add(m);
                }
            }
            return totallist;
        }

        /// <summary>
        ///（专用于边缘采集器）按照TXT来分类并返回分类好的CSV文件组，统一为MergeClassifyModal类，有起始终止index等信息
        /// </summary>
        /// <param name="allfile">FileInfo类型文件组</param>
        /// <returns></returns>
        public static List<MergeClassifyModal> YXHClassifyFilebyTxt(this FileInfo[] allfile)
        {
            //创建一个totallist，用于存储每2个txt中间的数据的list
            List<MergeClassifyModal> totallist = new List<MergeClassifyModal>();
            List<int> txtindex = new List<int>();
            //这里也要判断一下csvindex是否有数据
            List<int> csvindex = new List<int>();
            for (int i = 0; i < allfile.Length; i++)
            {
                if (allfile[i].Extension.ToLower() == ".txt")
                {
                    txtindex.Add(i);
                }
                if (allfile[i].Extension.ToLower() == ".csv")
                {
                    csvindex.Add(i);
                }
            }
            //txt和csv有1个以上
            if (csvindex.Count > 0)
            {
                if(txtindex.Count > 0)
                {
                    //添加第一个list，用于存放第一个txt之前的csv数据，不管有没有csv
                    totallist.Add(new MergeClassifyModal());
                    for (int j = 0; j < txtindex[0]; j++)
                    {
                        totallist[0].filelist?.Add(allfile[j]);
                        totallist[0].endrowindex = 0;
                        totallist[0].startrowindex = 0;
                        totallist[0].endfileindex = 0;
                        totallist[0].startfileindex = 0;
                        totallist[0].endfileextramileage = 0;
                        //获取文件的数据行数
                        var rowcounts = allfile[j].FullName.GetCsvRowDataCount();
                        totallist[0].csvrowcount.Add(rowcounts);
                    }
                    for (int i = 0; i < txtindex.Count - 1; i++)
                    {

                        //如果第一个数的后一个数比第二个数前一个数小，就说明中间有csv需要合并
                        if (txtindex[i] + 1 <= txtindex[i + 1] - 1)
                        {
                            //中间有csv才添加，否则全是0的数据
                            totallist.Add(new MergeClassifyModal());
                            for (int j = txtindex[i] + 1; j <= txtindex[i + 1] - 1; j++)
                            {
                                totallist[totallist.Count - 1].filelist?.Add(allfile[j]);//这里totallist[i+1]是因为totallist[0]已经有了，必须从1开始
                                totallist[totallist.Count - 1].endrowindex = 0;
                                totallist[totallist.Count - 1].startrowindex = 0;
                                totallist[totallist.Count - 1].endfileindex = 0;
                                totallist[totallist.Count - 1].startfileindex = 0;
                                totallist[totallist.Count - 1].endfileextramileage = 0;
                                //获取文件的数据行数
                                var rowcounts = allfile[j].FullName.GetCsvRowDataCount();
                                totallist[totallist.Count - 1].csvrowcount.Add(rowcounts);
                            }
                        }
                    }
                    //判断最后一个txt的序号是否已经大于csv的文件总数了，说明后面没有csv了，就不要再添加了
                    if (txtindex[txtindex.Count - 1] < csvindex[csvindex.Count - 1])
                    {
                        //添加最后一个list，用于存放最后一个txt之后的csv数据，不管有没有csv
                        totallist.Add(new MergeClassifyModal());
                        for (int j = txtindex[txtindex.Count - 1] + 1; j < allfile.Length; j++)
                        {
                            totallist[totallist.Count - 1].filelist?.Add(allfile[j]);
                            totallist[totallist.Count - 1].endrowindex = 0;
                            totallist[totallist.Count - 1].startrowindex = 0;
                            totallist[totallist.Count - 1].endfileindex = 0;
                            totallist[totallist.Count - 1].startfileindex = 0;
                            totallist[totallist.Count - 1].endfileextramileage = 0;
                            //获取文件的数据行数
                            var rowcounts = allfile[j].FullName.GetCsvRowDataCount();
                            totallist[totallist.Count - 1].csvrowcount.Add(rowcounts);
                        }
                    }
                }
                //如果没有txt，那就直接把所有的csv文件放在totallist[0]里进行合并
                else
                {
                    totallist.Add(new MergeClassifyModal());
                    for (int j = 0; j < allfile.Length; j++)
                    {
                        totallist[0].filelist?.Add(allfile[j]);
                        totallist[0].endrowindex = 0;
                        totallist[0].startrowindex = 0;
                        totallist[0].endfileindex = 0;
                        totallist[0].startfileindex = 0;
                        totallist[0].endfileextramileage = 0;
                        //获取文件的数据行数
                        var rowcounts = allfile[j].FullName.GetCsvRowDataCount();
                        totallist[0].csvrowcount.Add(rowcounts);
                    }
                }
            }
           
            return totallist;
        }

        /// <summary>
        /// 获得所有 filelist 数量大于 60 的 FileInfo 组合成的数组了
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static FileInfo[] YXHSelectLessThan60(this List<MergeClassifyModal> data)
        {
            // 选择出所有 filelist 数量大于 60 的 MergeClassifyModal 实例  
            var selectedFileLists = data
                .Where(modal => modal.filelist.Count <61)
                .SelectMany(modal => modal.filelist);
            // 转换成数组并返回  
            return selectedFileLists.ToArray();
        }

        /// <summary>
        /// （专用于边缘采集器）按照时间顺序来分类并返回分类好的CSV文件组，统一为MileageClassifyClass类，有起始终止index等信息
        /// </summary>
        /// <param name="files">FileInfo类型文件组</param>
        /// <returns>List<MileageClassifyClass></returns>
        public static List<MergeClassifyModal> YXHClassifyFilebyTime(this FileInfo[] files)
        {
            //创建一个totallist，用于存储每半个小时的list
            List<MergeClassifyModal> totallist = new List<MergeClassifyModal>();
            //创建48个List<FileInfo>()，并添加到totallist
            for (int i = 0; i < 49; i++)
            {
                totallist.Add(new MergeClassifyModal());
            }
            if (files.Length > 0)
            {
                //用第一个文件来获取48个分割时间线，因为要用到第一个文件的文件名
                List<DateTime> timelist = files[0].YXHGetSplitTime();
                foreach (var file in files)
                {
                    //获得每个文件的数据行数，不包括标题行
                    var rowcounts = File.ReadAllLines(file.FullName).Length - 1;
                    //这里必须要 timelist.Count-1，否则会超限，因为timelist有49个，index只能到48，这里已经修复了最后一个文件是23点59分59秒的bug
                    for (int i = 0; i < timelist.Count-1; i++)
                    {
                        if (file.YXHFilenametoDateTime() >= timelist[i] && file.YXHFilenametoDateTime() <= timelist[i + 1])
                        {
                           
                            totallist[i].filelist?.Add(file);
                            totallist[i].endrowindex = 0;
                            totallist[i].startrowindex = 0;
                            totallist[i].endfileindex = 0;
                            totallist[i].startfileindex = 0;
                            totallist[i].endfileextramileage = 0;
                            totallist[i].csvrowcount.Add(rowcounts);
                        }
                    }
                }
            }
            return totallist;
        }

        /// <summary>
        /// （专用于边缘采集器）按照基准里程来分类并返回分类好的MileageClassifyClass,统一为MileageClassifyClass类，有起始终止index等信息
        /// </summary>
        /// <param name="files"></param>
        /// <param name="permileage"></param>
        /// <param name="spdindex"></param>
        /// <returns></returns>
        public static List<MergeClassifyModal> YXHClassifyFilebyMileage(this FileInfo[] files, int permileage, int spdindex)
        {
            //创建存储每个文件的里程数的list
            List<double> cummileages = new List<double>();
            foreach (var file in files)
            {
                
                //获得速度list
                var spdlist = CSVHelperClass.GetDataFromColumnIdx(file, spdindex);
                //获得timelist
                var timelist = CSVHelperClass.GetDataFromColumnIdx(file, 0);
                if (spdlist != null && timelist != null)
                {
                    cummileages.Add(Mileage.ReturnSumDistanceperFile(spdlist, timelist));//把每个文件的里程数添加到这个mileageslist里面
                }
            }
            List<MergeClassifyModal> result = new List<MergeClassifyModal>();
            var startfileindex = 0;
            var sum = 0.0;
            for (var i = 0; i < cummileages.Count; i++)
            {
                sum += cummileages[i];
                //先判断是否超过标准里程-5的差值，超过了才能进行下面的分割判断，没超过继续叠加
                if (sum >= permileage - 5)
                {
                    //如果叠加里程达到标准单位里程的差值5米以内则不会分割单个文件
                    if (Math.Abs(sum - permileage) <= 5)
                    {
                        //先判断result里面有没有数据，没有数据说明是第一次叠加里程，不用考虑起始文件的行数
                        if (result.Count == 0)
                        {
                            //倒数第二第三个数字表示起始文件的行数和终止文件的行数，都等于0表示包含所有行，不会分割单个文件，最后一个数表示终止文件分割的里程
                            //result.Add(new List<int> { startfileindex, i, 0, 0, 0 });
                            //result.Add(new MileageClassifyClass());
                            MergeClassifyModal mlist = new MergeClassifyModal();
                            mlist.endrowindex = 0;
                            mlist.startrowindex = 0;
                            mlist.endfileindex = i;
                            mlist.startfileindex = startfileindex;
                            mlist.endfileextramileage = 0;

                            for (int j = mlist.startfileindex; j <= mlist.endfileindex; j++)
                            {
                                //获取文件的数据行数
                                var rowcounts = File.ReadAllLines(files[j].FullName).Length - 1;
                                mlist.csvrowcount.Add(rowcounts);
                                mlist.filelist?.Add(files[j]);
                            }
                            result.Add(mlist);
                            //startfileindex = i + 1;//这里由于终止判断了不会分割，所以下一次的起始文件index加1
                            //sum = 0;
                        }
                        //要考虑上一次是否有分割过
                        else
                        {
                            //如果前一个数据的终止文件行数有值，说明前面一个数据有分割过，那这一次的起始文件还是原来上一次的终止文件
                            if (result[result.Count - 1].endrowindex > 0)
                            {
                                MergeClassifyModal mlist = new MergeClassifyModal();
                                mlist.endrowindex = 0;
                                mlist.startrowindex = result[result.Count - 1].endrowindex;
                                mlist.endfileindex = i;
                                mlist.startfileindex = result[result.Count - 1].endfileindex;
                                mlist.endfileextramileage = 0;
                                for (int j = mlist.startfileindex; j <= mlist.endfileindex; j++)
                                {
                                    //获取文件的数据行数
                                    var rowcounts = File.ReadAllLines(files[j].FullName).Length - 1;
                                    mlist.csvrowcount.Add(rowcounts);
                                    mlist.filelist?.Add(files[j]);
                                }
                                result.Add(mlist);
                            }
                            //说明前一个数据的终止文件行数为0，没有分割过，那这一次的起始文件就是startfileindex
                            else
                            {
                                MergeClassifyModal mlist = new MergeClassifyModal();
                                mlist.endrowindex = 0;
                                mlist.startrowindex = 0;
                                mlist.endfileindex = i;
                                mlist.startfileindex = startfileindex;
                                mlist.endfileextramileage = 0;
                                for (int j = mlist.startfileindex; j <= mlist.endfileindex; j++)
                                {
                                    //获取文件的数据行数
                                    var rowcounts = File.ReadAllLines(files[j].FullName).Length - 1;
                                    mlist.csvrowcount.Add(rowcounts);
                                    mlist.filelist?.Add(files[j]);
                                }
                                result.Add(mlist);
                            }

                        }
                        startfileindex = i + 1;//这里由于终止文件判断了不会分割，所以下一次的起始文件index加1
                        sum = 0;
                    }
                    //差值超过5就会分割文件
                    else
                    {

                        //获得准确差值
                        var diff = sum - permileage;
                        //获得要分割文件的速度cummileages
                        var spdcummileages = CSVHelperClass.GetDataFromColumnIdx(files[i], spdindex);
                        //获得要分割文件的timecummileages
                        var timecummileages = CSVHelperClass.GetDataFromColumnIdx(files[i], 0);
                        if (spdcummileages != null && timecummileages != null)
                        {
                            var mileage = Mileage.ReturnSingleDistanceperFile(spdcummileages, timecummileages);//计算这个文件的每行里程数
                            var singlesum = 0.0;
                            //倒序循环叠加每行里程，如超过差值停止循环，找到此文件要分割的行数
                            for (int j = mileage.Count - 1; j >= 0; j--)
                            {
                                singlesum += mileage[j];
                                if (singlesum >= diff)
                                {
                                    if (result.Count == 0)
                                    {
                                        MergeClassifyModal mlist = new MergeClassifyModal();
                                        mlist.endrowindex = j;
                                        mlist.startrowindex = 0;
                                        mlist.endfileindex = i;
                                        mlist.startfileindex = startfileindex;
                                        mlist.endfileextramileage = diff;
                                        for (int k = mlist.startfileindex; k <= mlist.endfileindex; k++)
                                        {
                                            //获取文件的数据行数
                                            var rowcounts = File.ReadAllLines(files[k].FullName).Length - 1;
                                            mlist.csvrowcount.Add(rowcounts);
                                            mlist.filelist?.Add(files[k]);
                                        }
                                        result.Add(mlist);
                                        //result.Add(new List<int> { startfileindex, i, 0, j, Convert.ToInt32(diff) });

                                    }
                                    //说明result之前有值
                                    else
                                    {
                                        //如果前一个数据的终止文件行数有值，说明前面一个数据有分割过，那这一次的起始文件还是原来上一次的终止文件
                                        if (result[result.Count - 1].endrowindex > 0)
                                        {

                                            //这一次的起始文件为上一次的终止文件，终止文件为i，起始行数为前一次的终止文件的行数，终止文件的行数为j，里程差值为diff
                                            MergeClassifyModal mlist = new MergeClassifyModal();
                                            mlist.endrowindex = j;
                                            mlist.startrowindex = result[result.Count - 1].endrowindex;
                                            mlist.endfileindex = i;
                                            mlist.startfileindex = result[result.Count - 1].endfileindex;
                                            mlist.endfileextramileage = diff;
                                            for (int k = mlist.startfileindex; k <= mlist.endfileindex; k++)
                                            {
                                                //获取文件的数据行数
                                                var rowcounts = File.ReadAllLines(files[k].FullName).Length - 1;
                                                mlist.csvrowcount.Add(rowcounts);
                                                mlist.filelist?.Add(files[k]);
                                            }
                                            result.Add(mlist);
                                            //result.Add(new List<int> { result[result.Count - 1][1], i, result[result.Count - 1][3], j, Convert.ToInt32(diff) });
                                        }
                                        else
                                        {
                                            MergeClassifyModal mlist = new MergeClassifyModal();
                                            mlist.endrowindex = j;
                                            mlist.startrowindex = 0;
                                            mlist.endfileindex = i;
                                            mlist.startfileindex = startfileindex;
                                            mlist.endfileextramileage = diff;
                                            for (int k = mlist.startfileindex; k <= mlist.endfileindex; k++)
                                            {
                                                //获取文件的数据行数
                                                var rowcounts = File.ReadAllLines(files[k].FullName).Length - 1;
                                                mlist.csvrowcount.Add(rowcounts);
                                                mlist.filelist?.Add(files[k]);
                                            }
                                            result.Add(mlist);
                                            //result.Add(new List<int> { startfileindex, i, 0, j, Convert.ToInt32(diff) });
                                        }

                                    }
                                    startfileindex = i;//由于这一次分割过，所以下一次的起始文件还是i
                                    sum = diff;//由于这一次分割过，所以下一次的sum计数要从diff算起
                                    break;//停止循环
                                }

                            }


                        }

                    }
                }

            }
            //文件循环结束后startfileindex没有到最后一个文件时，说明合并完后还有剩余文件加起来没有到标准里程
            if (startfileindex < cummileages.Count)
            {
                //说明所有文件加起来的里程没有超过1km
                if (result.Count == 0)
                {
                    MergeClassifyModal mlist = new MergeClassifyModal();
                    mlist.endrowindex = 0;
                    mlist.startrowindex = 0;
                    mlist.endfileindex = cummileages.Count - 1;
                    mlist.startfileindex = startfileindex;
                    mlist.endfileextramileage = 0;
                    for (int j = mlist.startfileindex; j <= mlist.endfileindex; j++)
                    {
                        //获取文件的数据行数
                        var rowcounts = File.ReadAllLines(files[j].FullName).Length - 1;
                        mlist.csvrowcount.Add(rowcounts);
                        mlist.filelist?.Add(files[j]);
                    }
                    result.Add(mlist);
                    //result.Add(new List<int> { startfileindex, cummileages.Count - 1, 0, 0, 0 });
                }
                else
                {
                    //说明前面一组有分割
                    if (result[result.Count - 1].endrowindex > 0)
                    {
                        MergeClassifyModal mlist = new MergeClassifyModal();
                        mlist.endrowindex = 0;
                        mlist.startrowindex = result[result.Count - 1].endrowindex;
                        mlist.endfileindex = cummileages.Count - 1;
                        mlist.startfileindex = result[result.Count - 1].endfileindex;
                        mlist.endfileextramileage = 0;
                        for (int k = mlist.startfileindex; k <= mlist.endfileindex; k++)
                        {
                            //获取文件的数据行数
                            var rowcounts = File.ReadAllLines(files[k].FullName).Length - 1;
                            mlist.csvrowcount.Add(rowcounts);
                            mlist.filelist?.Add(files[k]);
                        }
                        result.Add(mlist);
                        //result.Add(new List<int> { result[result.Count - 1][1], cummileages.Count - 1, result[result.Count - 1][3], 0, 0 });

                    }
                    //说明前面一组没有分割
                    else
                    {
                        MergeClassifyModal mlist = new MergeClassifyModal();
                        mlist.endrowindex = 0;
                        mlist.startrowindex = 0;
                        mlist.endfileindex = cummileages.Count - 1;
                        mlist.startfileindex = startfileindex;
                        mlist.endfileextramileage = 0;
                        for (int j = mlist.startfileindex; j <= mlist.endfileindex; j++)
                        {
                            //获取文件的数据行数
                            var rowcounts = File.ReadAllLines(files[j].FullName).Length - 1;
                            mlist.csvrowcount.Add(rowcounts);
                            mlist.filelist?.Add(files[j]);
                        }
                        result.Add(mlist);
                        //result.Add(new List<int> { startfileindex, cummileages.Count - 1, 0, 0, 0 });
                    }
                }
            }
            return result;

        }

        public static List<MergeClassifyModal>? YXHClassifyFilebyMileagebySpdName(this FileInfo[] files, int permileage, string spdname)
        {
            //创建存储每个文件的里程数的list
            List<double> cummileages = new List<double>();
            foreach (var file in files)
            {
                //获得速度list
                var spdlist = CSVHelperClass.GetDataFromColumnName(file, spdname);
                //获得timelist
                var timelist = CSVHelperClass.GetDataFromColumnIdx(file, 0);
                if (spdlist.Count>0 && timelist.Count>0)
                {
                    cummileages.Add(Mileage.ReturnSumDistanceperFile(spdlist, timelist));//把每个文件的里程数添加到这个mileageslist里面
                }
                else
                {
                    return null;
                }
            }
            List<MergeClassifyModal> result = new List<MergeClassifyModal>();
            var startfileindex = 0;
            var sum = 0.0;
            for (var i = 0; i < cummileages.Count; i++)
            {
                sum += cummileages[i];
                //先判断是否超过标准里程-5的差值，超过了才能进行下面的分割判断，没超过继续叠加
                if (sum >= permileage - 5)
                {
                    //如果叠加里程达到标准单位里程的差值5米以内则不会分割单个文件
                    if (Math.Abs(sum - permileage) <= 5)
                    {
                        //先判断result里面有没有数据，没有数据说明是第一次叠加里程，不用考虑起始文件的行数
                        if (result.Count == 0)
                        {
                            //倒数第二第三个数字表示起始文件的行数和终止文件的行数，都等于0表示包含所有行，不会分割单个文件，最后一个数表示终止文件分割的里程
                            //result.Add(new List<int> { startfileindex, i, 0, 0, 0 });
                            //result.Add(new MileageClassifyClass());
                            MergeClassifyModal mlist = new MergeClassifyModal();
                            mlist.endrowindex = 0;
                            mlist.startrowindex = 0;
                            mlist.endfileindex = i;
                            mlist.startfileindex = startfileindex;
                            mlist.endfileextramileage = 0;

                            for (int j = mlist.startfileindex; j <= mlist.endfileindex; j++)
                            {
                                mlist.filelist?.Add(files[j]);
                            }
                            result.Add(mlist);
                            //startfileindex = i + 1;//这里由于终止判断了不会分割，所以下一次的起始文件index加1
                            //sum = 0;
                        }
                        //要考虑上一次是否有分割过
                        else
                        {
                            //如果前一个数据的终止文件行数有值，说明前面一个数据有分割过，那这一次的起始文件还是原来上一次的终止文件
                            if (result[result.Count - 1].endrowindex > 0)
                            {
                                MergeClassifyModal mlist = new MergeClassifyModal();
                                mlist.endrowindex = 0;
                                mlist.startrowindex = result[result.Count - 1].endrowindex;
                                mlist.endfileindex = i;
                                mlist.startfileindex = result[result.Count - 1].endfileindex;
                                mlist.endfileextramileage = 0;
                                for (int j = mlist.startfileindex; j <= mlist.endfileindex; j++)
                                {
                                    mlist.filelist?.Add(files[j]);
                                }
                                result.Add(mlist);
                            }
                            //说明前一个数据的终止文件行数为0，没有分割过，那这一次的起始文件就是startfileindex
                            else
                            {
                                MergeClassifyModal mlist = new MergeClassifyModal();
                                mlist.endrowindex = 0;
                                mlist.startrowindex = 0;
                                mlist.endfileindex = i;
                                mlist.startfileindex = startfileindex;
                                mlist.endfileextramileage = 0;
                                for (int j = mlist.startfileindex; j <= mlist.endfileindex; j++)
                                {
                                    mlist.filelist?.Add(files[j]);
                                }
                                result.Add(mlist);
                            }

                        }
                        startfileindex = i + 1;//这里由于终止文件判断了不会分割，所以下一次的起始文件index加1
                        sum = 0;
                    }
                    //差值超过5就会分割文件
                    else
                    {

                        //获得准确差值
                        var diff = sum - permileage;
                        //获得要分割文件的速度cummileages
                        var spdcummileages = CSVHelperClass.GetDataFromColumnName(files[i], spdname);
                        //获得要分割文件的timecummileages
                        var timecummileages = CSVHelperClass.GetDataFromColumnIdx(files[i], 0);
                        if (spdcummileages != null && timecummileages != null)
                        {
                            var mileage = Mileage.ReturnSingleDistanceperFile(spdcummileages, timecummileages);//计算这个文件的每行里程数
                            var singlesum = 0.0;
                            //倒序循环叠加每行里程，如超过差值停止循环，找到此文件要分割的行数
                            for (int j = mileage.Count - 1; j >= 0; j--)
                            {
                                singlesum += mileage[j];
                                if (singlesum >= diff)
                                {
                                    if (result.Count == 0)
                                    {
                                        MergeClassifyModal mlist = new MergeClassifyModal();
                                        mlist.endrowindex = j;
                                        mlist.startrowindex = 0;
                                        mlist.endfileindex = i;
                                        mlist.startfileindex = startfileindex;
                                        mlist.endfileextramileage = diff;
                                        for (int k = mlist.startfileindex; k <= mlist.endfileindex; k++)
                                        {
                                            mlist.filelist?.Add(files[k]);
                                        }
                                        result.Add(mlist);
                                        //result.Add(new List<int> { startfileindex, i, 0, j, Convert.ToInt32(diff) });

                                    }
                                    //说明result之前有值
                                    else
                                    {
                                        //如果前一个数据的终止文件行数有值，说明前面一个数据有分割过，那这一次的起始文件还是原来上一次的终止文件
                                        if (result[result.Count - 1].endrowindex > 0)
                                        {

                                            //这一次的起始文件为上一次的终止文件，终止文件为i，起始行数为前一次的终止文件的行数，终止文件的行数为j，里程差值为diff
                                            MergeClassifyModal mlist = new MergeClassifyModal();
                                            mlist.endrowindex = j;
                                            mlist.startrowindex = result[result.Count - 1].endrowindex;
                                            mlist.endfileindex = i;
                                            mlist.startfileindex = result[result.Count - 1].endfileindex;
                                            mlist.endfileextramileage = diff;
                                            for (int k = mlist.startfileindex; k <= mlist.endfileindex; k++)
                                            {
                                                mlist.filelist?.Add(files[k]);
                                            }
                                            result.Add(mlist);
                                            //result.Add(new List<int> { result[result.Count - 1][1], i, result[result.Count - 1][3], j, Convert.ToInt32(diff) });
                                        }
                                        else
                                        {
                                            MergeClassifyModal mlist = new MergeClassifyModal();
                                            mlist.endrowindex = j;
                                            mlist.startrowindex = 0;
                                            mlist.endfileindex = i;
                                            mlist.startfileindex = startfileindex;
                                            mlist.endfileextramileage = diff;
                                            for (int k = mlist.startfileindex; k <= mlist.endfileindex; k++)
                                            {
                                                mlist.filelist?.Add(files[k]);
                                            }
                                            result.Add(mlist);
                                            //result.Add(new List<int> { startfileindex, i, 0, j, Convert.ToInt32(diff) });
                                        }

                                    }
                                    startfileindex = i;//由于这一次分割过，所以下一次的起始文件还是i
                                    sum = diff;//由于这一次分割过，所以下一次的sum计数要从diff算起
                                    break;//停止循环
                                }

                            }


                        }

                    }
                }

            }
            //文件循环结束后startfileindex没有到最后一个文件时，说明合并完后还有剩余文件加起来没有到标准里程
            if (startfileindex < cummileages.Count)
            {
                //说明所有文件加起来的里程没有超过1km
                if (result.Count == 0)
                {
                    MergeClassifyModal mlist = new MergeClassifyModal();
                    mlist.endrowindex = 0;
                    mlist.startrowindex = 0;
                    mlist.endfileindex = cummileages.Count - 1;
                    mlist.startfileindex = startfileindex;
                    mlist.endfileextramileage = 0;
                    for (int j = mlist.startfileindex; j <= mlist.endfileindex; j++)
                    {
                        mlist.filelist?.Add(files[j]);
                    }
                    result.Add(mlist);
                    //result.Add(new List<int> { startfileindex, cummileages.Count - 1, 0, 0, 0 });
                }
                else
                {
                    //说明前面一组有分割
                    if (result[result.Count - 1].endrowindex > 0)
                    {
                        MergeClassifyModal mlist = new MergeClassifyModal();
                        mlist.endrowindex = 0;
                        mlist.startrowindex = result[result.Count - 1].endrowindex;
                        mlist.endfileindex = cummileages.Count - 1;
                        mlist.startfileindex = result[result.Count - 1].endfileindex;
                        mlist.endfileextramileage = 0;
                        for (int k = mlist.startfileindex; k <= mlist.endfileindex; k++)
                        {
                            mlist.filelist?.Add(files[k]);
                        }
                        result.Add(mlist);
                        //result.Add(new List<int> { result[result.Count - 1][1], cummileages.Count - 1, result[result.Count - 1][3], 0, 0 });

                    }
                    //说明前面一组没有分割
                    else
                    {
                        MergeClassifyModal mlist = new MergeClassifyModal();
                        mlist.endrowindex = 0;
                        mlist.startrowindex = 0;
                        mlist.endfileindex = cummileages.Count - 1;
                        mlist.startfileindex = startfileindex;
                        mlist.endfileextramileage = 0;
                        for (int j = mlist.startfileindex; j <= mlist.endfileindex; j++)
                        {
                            mlist.filelist?.Add(files[j]);
                        }
                        result.Add(mlist);
                        //result.Add(new List<int> { startfileindex, cummileages.Count - 1, 0, 0, 0 });
                    }
                }
            }
            return result;

        }

        /// <summary>
        /// （专用于边缘采集器）根据合并类型来获得不同的分类好的文件列表，添加了精准里程分割的功能
        /// </summary>
        /// <param name="d">需要分类的文件夹</param>
        /// <param name="mergetype">合并方式：1为按时间合并，0为按txt文件合并，为2则为里程合并</param>
        /// <param name="permileage">里程合并的基准里程</param>
        /// <param name="spdindex">里程合并的指定csv里的车速index</param>
        public static List<MergeClassifyModal> YXHGetClassifyFileListperFolder(DirectoryInfo d, mergetype mergetype, int permileage, int spdindex)
        {
            List<MergeClassifyModal> classifylist = new List<MergeClassifyModal>();
            //获取每一个子文件夹的所有csv文件，并按文件名排序
            var allfiles = BasicFilesOperation.YXHGetAllextFiles(d.FullName, "*.csv");
            //按时间合并
            if ((int)mergetype == 1)
            {
                //每半个小时分类汇总成一个总classifylist，每个list里有各自相同时间段的数据
                classifylist = YXHClassifyFilebyTime(allfiles);
            }
            //按里程来分布
            else if ((int)mergetype == 2)
            {
                //每多少公里分类汇总成一个总classifylist，每个list里有各自相同里程段的数据
                classifylist = YXHClassifyFilebyMileage(allfiles, permileage, spdindex);
            }
            else
            {
                var txtallfiles = BasicFilesOperation.YXHGetAllextFiles(d.FullName, "*.*").Where(s => s.Name.ToLower().EndsWith(".csv") || s.Name.ToLower().EndsWith(".txt")).ToArray();
                //每半个小时分类汇总成一个总classifylist，每个list里有各自相同时间段的数据
                classifylist = YXHClassifyFilebyTxt(txtallfiles);
            }
            return classifylist;
        }
        /// <summary>
        /// 利用速度序号改为利用速度列名（目前使用的方法）
        /// </summary>
        /// <param name="d"></param>
        /// <param name="mergetype"></param>
        /// <param name="permileage"></param>
        /// <param name="spdname"></param>
        /// <returns></returns>
        public static List<MergeClassifyModal>? YXHGetClassifyFileListperFolderSpdName(DirectoryInfo d, mergetype mergetype, int permileage, string spdname)
        {
            List<MergeClassifyModal>? classifylist = new List<MergeClassifyModal>();
            //获取每一个子文件夹的所有csv文件，并按文件名排序
            var allfiles = BasicFilesOperation.YXHGetAllextFiles(d.FullName, "*.csv");
            //按时间合并
            if ((int)mergetype == 1)
            {
                //每半个小时分类汇总成一个总classifylist，每个list里有各自相同时间段的数据
                classifylist = YXHClassifyFilebyTime(allfiles);
            }
            //按里程来分布
            else if ((int)mergetype == 2)
            {
                //里程合并还是用index来标识速度通道较快，但前端还是用名称来标识，所以需要中间转一下，从名称到index，而且只需要转一次就行
                //先获得第一个csv文件
                if (allfiles.Length > 0)
                {
                    //获得标题
                    var headers= CSVHelperClass.GetHeader(allfiles.First().FullName);
                    int spdidx = Array.IndexOf(headers, spdname);
                    if(spdidx == -1)
                    {
                        return null;
                    }
                    //每多少公里分类汇总成一个总classifylist，每个list里有各自相同里程段的数据
                    classifylist = YXHClassifyFilebyMileage(allfiles, permileage, spdidx);
                }
                else { return null; }
            }
            else
            {
                var txtallfiles = BasicFilesOperation.YXHGetAllextFiles(d.FullName, "*.*").Where(s => s.Name.ToLower().EndsWith(".csv") || s.Name.ToLower().EndsWith(".txt")).ToArray();
                //每半个小时分类汇总成一个总classifylist，每个list里有各自相同时间段的数据
                classifylist = YXHClassifyFilebyTxt(txtallfiles);
            }
            return classifylist;
        }
        /// <summary>
        /// （专用于边缘采集器）获得每个分类list合并后的文件路径(需要目录）
        /// </summary>
        /// <param name="d"></param>
        /// <param name="filelist"></param>
        /// <param name="combinedpath"></param>
        /// <param name="combinetype"></param>
        /// <returns></returns>
        public static string YXHGetCombinedFileName(DirectoryInfo d, List<FileInfo> filelist, string rdpath, mergetype combinetype)
        {
            string combinefilepath = "";
            try
            {
                string filedate = d.Name;
                //获取合并后的指定的日期文件夹路径
                string finalcombinedpath = Path.Combine(rdpath, filedate);
                if (filelist.Count > 0)
                {
                    //时间模式
                    if ((int)combinetype == 1)
                    {
                        combinefilepath = Path.Combine(finalcombinedpath, filelist[0].YXHGetMergeFilenamebyhalfhour());
                    }
                    else
                    {
                        if (filelist.Count > 1)
                        {
                            combinefilepath = Path.Combine(finalcombinedpath, filelist[0].YXHGetMergeFilenamebyTxt(filelist[filelist.Count - 1]));
                        }
                        else
                        {
                            combinefilepath = Path.Combine(finalcombinedpath, filelist[0].Name);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string methodName = "";
                // 获取堆栈跟踪信息
                StackTrace stackTrace = new StackTrace(ex);
                // 获取第一个堆栈帧
                StackFrame? stackFrame = stackTrace.GetFrame(0);
                // 获取方法名
                if (stackFrame != null)
                {
                    var s = stackFrame.GetMethod();
                    if (s != null)
                    {
                        methodName = s.Name;
                    }
                }
                Console.WriteLine(methodName + "方法出现了错误：" + ex.StackTrace);
            }
            return combinefilepath;
        }
        /// <summary>
        /// （专用于边缘采集器）获得每个分类list合并后的文件路径（不需要目录）
        /// </summary>
        /// <param name="filelist"></param>
        /// <param name="rdpath"></param>
        /// <param name="combinetype"></param>
        /// <returns></returns>
        public static string YXHGetCombinedFileName(List<FileInfo> filelist, string rdpath, mergetype combinetype)
        {
            string combinefilepath = "";
            try
            {
                if (filelist.Count > 0)
                {
                    //时间模式
                    if ((int)combinetype == 1)
                    {
                        combinefilepath = Path.Combine(rdpath, filelist[0].YXHGetMergeFilenamebyhalfhour());
                    }
                    else
                    {
                        if (filelist.Count > 1)
                        {
                            combinefilepath = Path.Combine(rdpath, filelist[0].YXHGetMergeFilenamebyTxt(filelist[filelist.Count - 1]));
                        }
                        else
                        {
                            combinefilepath = Path.Combine(rdpath, filelist[0].Name);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string methodName = "";
                // 获取堆栈跟踪信息
                StackTrace stackTrace = new StackTrace(ex);
                // 获取第一个堆栈帧
                StackFrame? stackFrame = stackTrace.GetFrame(0);
                // 获取方法名
                if (stackFrame != null)
                {
                    var s = stackFrame.GetMethod();
                    if (s != null)
                    {
                        methodName = s.Name;
                    }
                }
                Console.WriteLine(methodName + "方法出现了错误：" + ex.StackTrace);
            }
            return combinefilepath;
        }
        /// <summary>
        /// （专用于边缘采集器）根据combinetype来执行合并操作，添加了精确里程分布的功能
        /// </summary>
        /// <param name="originalpath"></param>
        /// <param name="combinedpath"></param>
        /// <param name="configuredfolders"></param>
        /// <param name="combinetype"></param>
        /// <param name="permileage"></param>
        /// <param name="spdindex"></param>
        public static async Task<List<string>> YXHExcuteMergeAsync(MergeModal row)
        {
            List<string> list = new List<string>();//专用于输出程序计算过程中的错误信息
            try
            {
                await Task.Run(() =>
                {
                    //先创建输出文件夹
                    Directory.CreateDirectory(row.rdpath);
                    //获得需要合并的源数据日期文件夹列表
                    var ds = YXHGetProcessedDateDir(row.odpath, row.rdpath, row.datedir);
                    if (ds != null)
                    {
                        if (ds.Length > 0)
                        {
                            Stopwatch timer = new Stopwatch();
                            timer.Start();
                            foreach (var d in ds)
                            {
                                //获得分类好的文件列表
                                var classifiedlist = YXHGetClassifyFileListperFolderSpdName(d, row.mergetype, row.permileage, row.spdname);
                                if (classifiedlist?.Count > 0)
                                {
                                    ParallelOptions options = new ParallelOptions();
                                    options.MaxDegreeOfParallelism = row.threads;//支持threadsnum个并发执行,控制数量
                                    var r = Parallel.ForEach(classifiedlist, options, c =>
                                    {
                                        //获得输出的文件路径
                                        var outputpath = YXHGetCombinedFileName(d, c.filelist, row.rdpath, row.mergetype);
                                        //执行合并csv动作
                                        CSVHelperClass.YXHMerge(c, outputpath);

                                    });
                                  
                                }
                                else
                                {
                                    list.Add($"{d.Name}无法分类,可能是通道名称不匹配");
                                }
                                //启动多线程

                            }
                            timer.Stop();
                            list.Add($"{row.mergetype}合并程序总完成时间为{timer.Elapsed.TotalMinutes.ToString("0.00")}分-单线程");
                        }
                        else
                        {
                            list.Add("目标路径下已有所有数据，不进行任何操作");
                        }
                    }
                    else
                    {
                        list.Add("目标路径有问题，请检查配置文件");
                    }
                });

            }
            catch (Exception ex)
            {
               
                string methodName = "";
                // 获取堆栈跟踪信息
                StackTrace stackTrace = new StackTrace(ex);
                // 获取第一个堆栈帧
                StackFrame? stackFrame = stackTrace.GetFrame(0);
                // 获取方法名
                if (stackFrame != null)
                {
                    var s = stackFrame.GetMethod();
                    if (s != null)
                    {
                        methodName = s.Name;
                    }
                }
                list.Add(methodName + "方法出现了错误：" + ex.StackTrace);
            }
            return list;
        }

        
        /// <summary>
        /// 目前使用的txt合并方法
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public static async Task<List<string>> YXHExcuteMergeMultiThreadAsync(MergeModal row)
        {
            List<string> list = new List<string>();//专用于输出程序计算过程中的错误信息
            try
            {
                await Task.Run( async () =>
                {
                    //先创建输出文件夹
                    Directory.CreateDirectory(row.rdpath);
                    //获得需要合并的源数据日期文件夹列表
                    var ds = YXHGetProcessedDateDir(row.odpath, row.rdpath, row.datedir);
                    if (ds != null)
                    {
                        if (ds.Length > 0)
                        {
                            Stopwatch timer = new Stopwatch();
                            timer.Start();
                            foreach (var d in ds)
                            {
                                Stopwatch Stimer = new Stopwatch();
                                Stimer.Start();
                                //获得分类好的文件列表
                                var classifiedlist = YXHGetClassifyFileListperFolderSpdName(d, row.mergetype, row.permileage, row.spdname);
                                if (classifiedlist?.Count > 0)
                                {
                                    List<Task> tasks = new List<Task>();
                                    foreach (var part in classifiedlist)
                                    {
                                        Task task = Task.Run(() =>
                                        {
                                            //获得输出的文件路径
                                            var outputpath = YXHGetCombinedFileName(d, part.filelist, row.rdpath, row.mergetype);
                                            //执行合并csv动作
                                            CSVHelperClass.YXHMerge(part, outputpath);
                                        });
                                        tasks.Add(task);
                                    }
                                    await Task.WhenAll(tasks.ToArray());
                                }
                                else
                                {
                                    list.Add($"{d.Name}无法分类,可能是通道名称不匹配或者没有csv文件");
                                }
                                Stimer.Stop();
                                list.Add($"{d.Name}{row.mergetype}合并完成时间为{Stimer.Elapsed.TotalMinutes:0.00}分");
                            }
                            timer.Stop();
                            list.Add($"{row.mergetype}合并程序总完成时间为{timer.Elapsed.TotalMinutes.ToString("0.00")}分-多线程");
                        }
                        else
                        {
                            list.Add("目标路径下已有所有数据，不进行任何操作");
                        }
                    }
                    else
                    {
                        list.Add("目标路径有问题，请检查配置文件");
                    }
                });

            }
            catch (Exception ex)
            {

                string methodName = "";
                // 获取堆栈跟踪信息
                StackTrace stackTrace = new StackTrace(ex);
                // 获取第一个堆栈帧
                StackFrame? stackFrame = stackTrace.GetFrame(0);
                // 获取方法名
                if (stackFrame != null)
                {
                    var s = stackFrame.GetMethod();
                    if (s != null)
                    {
                        methodName = s.Name;
                    }
                }
                list.Add(methodName + "方法出现了错误：" + ex.StackTrace);
            }
            return list;
        }
    }
}
